package com.atguigu.tingshu.user.pattren.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.pattren.DeliveryStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: jack
 * @date: 2025/10/14 19:16
 */
@Component(SystemConstant.ORDER_ITEM_TYPE_ALBUM)
public class AlbumDelivery implements DeliveryStrategy {

    private static final Logger log = LoggerFactory.getLogger(AlbumDelivery.class);
    @Autowired
    UserPaidAlbumMapper userPaidAlbumMapper;


    @Override
    public void delivery(UserPaidRecordVo userPaidRecordVo) {

        log.info("专辑发放权益");
        //1. 判断订单是否已经被处理
        //1.1 为用户发放权益
        Long count = userPaidAlbumMapper.selectCount(
                new LambdaQueryWrapper<UserPaidAlbum>()
                        .eq(UserPaidAlbum::getOrderNo, userPaidRecordVo.getOrderNo())
        );
        if (count > 0) {
            return;
        }
        //2.新增订单记录即可
        UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
        userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
        userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
        userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
        userPaidAlbumMapper.insert(userPaidAlbum);
    }
}
